import { store } from "@/store";
import { defineStore } from "pinia";
import PaymentAPI, { type PaymentType, type PaymentListQuery } from "@/api/payment/payment";

export const usePaymentStore = defineStore("payment", () => {
  const list = ref<PaymentType[]>([]);
  const totalCount = ref(0);
  const totalPage = ref(0);

  const loading = reactive({
    list: false,
    detail: false,
  });

  async function fetchList(params: PaymentListQuery) {
    loading.list = true;
    try {
      const res = await PaymentAPI.getPaymentList(params);
      list.value = res.records;
      totalCount.value = res.total_count;
      totalPage.value = res.total_page;
    } finally {
      loading.list = false;
    }
  }

  async function fetchDetail(id: number) {
    loading.detail = true;
    try {
      return await PaymentAPI.getPaymentDetail(id);
    } finally {
      loading.detail = false;
    }
  }

  return {
    list,
    totalCount,
    totalPage,
    loading,
    fetchList,
    fetchDetail,
  };
});

export const usePaymentStoreHook = () => {
  return usePaymentStore(store);
};
